import { Component, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppModalComponentBase } from '@shared/common/app-modal-component-base';
import { HtmlHelper } from '@shared/helpers/HtmlHelper';
import { CreateFlowFromInput, UpdateWorkForm, WorkFlowDesignServiceProxy, WorkFlowFormDto } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap/modal';
import { finalize } from 'rxjs/operators';

@Component({
  selector: 'flow-from-modal',
  templateUrl: './flow-from-modal.component.html'
})
export class FlowFromModalComponent extends AppModalComponentBase {

    @ViewChild('editModal', { static: true }) modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    saving = false;
    flowWorkId:string = '';
    workFlowFormDto:WorkFlowFormDto = new WorkFlowFormDto();
    constructor(
        injector: Injector,
        private _workFlowDesignServiceProxy:WorkFlowDesignServiceProxy
    ) {
        super(injector);
    }

    
    show(id: string): void {
        this.modalExpandOb.resetStates();
        this.flowWorkId = id;
        this.modal.show(); 
        this.init();
    }


    private init(){
        if (this.flowWorkId){
            this._workFlowDesignServiceProxy.getFlowForm(this.flowWorkId).subscribe(res=>{
                if (res.id){
                    this.workFlowFormDto = res;
                    this.workFlowFormDto.htmlData = HtmlHelper.htmlDecode(this.workFlowFormDto.htmlData);
                }
                else{
                    this.workFlowFormDto.workFlowId = this.flowWorkId;
                }
            });
        }
      
    }


    save(): void {
        this.saving = true;
        let htmlData = this.workFlowFormDto.htmlData;
        if (htmlData == '<br>'){
            htmlData = '';
        }
        htmlData = HtmlHelper.htmlEncode(htmlData);

        let updateWorkForm = new UpdateWorkForm();
        Object.assign(updateWorkForm,this.workFlowFormDto);
        updateWorkForm.htmlData = htmlData;
        if (this.workFlowFormDto.id){
            this._workFlowDesignServiceProxy.updateFlowForm(updateWorkForm).pipe(finalize(() => { this.saving = false; })).subscribe(res=>{
                this.notify.info(this.l('SavedSuccessfully'));
                this.saving = false;
                this.modalSave.emit(true);
                this.close();
            });
        }
        else{
            let createFlowFromInput = new CreateFlowFromInput();
            Object.assign(createFlowFromInput,this.workFlowFormDto);
            createFlowFromInput.htmlData = htmlData;
            this._workFlowDesignServiceProxy.createFlowForm(createFlowFromInput).pipe(finalize(() => { this.saving = false; })).subscribe(res=>{
                this.notify.info(this.l('SavedSuccessfully'));
                this.saving = false;
                this.modalSave.emit(true);
                this.close();
            });
        }
     
    }

    close(): void {
        this.modal.hide();
    }
 
}
